package com.example.fantacylib.ui

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable

import android.widget.TextView
import com.example.baseutils.NavigationBarUtil.hideNavigationBar
import com.example.fantacylib.R


class ConfirmDialog(context: Context) : AlertDialog(context) {
    private val TAG = "ConfirmDialog"
    var isShow = false
    private var canOut: Boolean = true
    private var callBack: IConfirmCallBack? = null
    interface  IConfirmCallBack {
        fun onConfirm()
        fun onCancel()

    }
    fun init(out: Boolean = true): ConfirmDialog {
        this.canOut = out
        Builder(context).create()
        setCanceledOnTouchOutside(canOut)
//        setCancelable(false)
//        val inflater = LayoutInflater.from(context)
//        val view = inflater.inflate(R.layout.dialog_progress, null)
//        setView(view)
        return this
    }

    fun showConfirm(callBack: IConfirmCallBack, out: Boolean = true) {
        this.canOut = out
        dismissDialog()
        setCanceledOnTouchOutside(canOut)
        isShow = true
        show()
        window?.let {
            hideNavigationBar(it)
            it.setContentView(R.layout.dialog_confirm)
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            it.findViewById<TextView>(R.id.btn_re_capture).setOnClickListener {
                callBack.onCancel()
                dismissDialog()
            }
            it.findViewById<TextView>(R.id.btn_confirm).setOnClickListener {
                callBack.onConfirm()
                dismissDialog()
            }
        }
    }

    override fun onBackPressed() {
        if (canOut)
            super.onBackPressed()

    }

    fun dismissDialog() {
        if (isShow) {
            canOut = false
            isShow = false
            dismiss()
        }
    }

}